// subHoliday/holidayApproval/holidayApproval.js
const appg = getApp().globalData
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key_value: null,
    result: [],
    approveLeaveList: [],
    page: {
      pageSize: 10,
      pageNum: 1,
      totalCount: 0
    }
  },
  // 多选
  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },
  // 获取审批列表
  async getApproveLeaveList() {
    wx.showLoading({
      title: '加载中',
    })
    const params = {
      uid: appg.userInfo.id,
      position_ID: appg.userInfo.position_ID,
      ...this.data.page
    }
    try {
      const json = await getApp().$axios("/getApproveLeaveList", "POST", params)
      wx.hideLoading()
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      this.setData({
        page: json.page,
        approveLeaveList: json.data
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 同意
  async agree() {
    const params = {
      ids: this.data.result,
      position_ID: appg.userInfo.position_ID,
    }
    if (this.data.result && this.data.result.length != 0) {
      Dialog.confirm({
          title: '提示',
          message: '请选择批量的操作',
          confirmButtonText: "批量同意",
          cancelButtonText: "批量拒绝"
        })
        .then( async () => {
          // on confirm
          params.status = 1
          wx.showLoading({
            title: '加载中',
          })
          const json = await getApp().$axios("/approveLeave", "POST", params)
          wx.hideLoading()
          if (json.status != 200) {
            wx.showToast({
              title: "审批出错了",
              icon: 'error'
            })
            return
          }
          wx.showToast({
            title: json.data,
          })
          this.initialization()
          setTimeout(() => {
            this.getApproveLeaveList()
          }, 500)
        })
        .catch(async () => {
          // on cancel
          params.status = 2
          const json = await getApp().$axios("/approveLeave", "POST", params)
          if (json.status != 200) {
            wx.showToast({
              title: "审批出错了",
              icon: 'error'
            })
            return
          }
          wx.showToast({
            title: json.data,
          })
          this.initialization()
          setTimeout(() => {
            this.getApproveLeaveList()
          }, 500)
        });
    } else {
      wx.showToast({
        title: '请选择审批的人',
        icon: 'none'
      })
    }
  },
  // 初始化page
  initialization() {
    this.setData({
      page: {
        pageSize: 10,
        pageNum: 1,
        totalCount: 0
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getApproveLeaveList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})